package com.dingding.gmall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingding.gmall.common.utils.PageUtils;
import com.dingding.gmall.common.utils.Query;
import com.dingding.gmall.member.dao.MemberDao;
import com.dingding.gmall.member.dto.MemberLoginDTO;
import com.dingding.gmall.member.dto.MemberRegistDTO;
import com.dingding.gmall.member.entity.MemberEntity;
import com.dingding.gmall.member.entity.MemberLevelEntity;
import com.dingding.gmall.member.exception.PhoneException;
import com.dingding.gmall.member.exception.UserNameException;
import com.dingding.gmall.member.service.MemberLevelService;
import com.dingding.gmall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public void regist(MemberRegistDTO memberRegistDTO) {
        MemberEntity entity = new MemberEntity();
        //设置会员默认等级
        MemberLevelEntity memberLevelEntity = memberLevelService.getDefaultLevel();
        entity.setLevelId(memberLevelEntity.getId());

        checkPhoneUnique(memberRegistDTO.getPhone());
        checkUserNameUnique(memberRegistDTO.getUsername());

        entity.setMobile(memberRegistDTO.getPhone());
        entity.setUsername(memberRegistDTO.getUsername());
        entity.setNickname(memberRegistDTO.getUsername());

        //密码加密存储
        String encode = new BCryptPasswordEncoder().encode(memberRegistDTO.getPassword());
        entity.setPassword(encode);

        this.save(entity);
    }

    @Override
    public void checkPhoneUnique(String phone) {
        int mobileCount = this.count(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (mobileCount > 0){
            throw new PhoneException();
        }
    }

    @Override
    public void checkUserNameUnique(String username) {
        int personCount = this.count(new QueryWrapper<MemberEntity>().eq("username", username));
        if (personCount > 0){
            throw new UserNameException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginDTO loginDTO) {
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<MemberEntity>().eq("username", loginDTO.getAccount()).or()
                .eq("mobile", loginDTO.getAccount());
        MemberEntity memberEntity = this.getOne(queryWrapper);
        if (memberEntity == null) {
            //登录失败
            return null;
        }
        //获取到数据库里的password
        String password = memberEntity.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //进行密码匹配
        boolean matches = passwordEncoder.matches(loginDTO.getPassword(), password);
        if (!matches) {
            //密码错误
            return null;
        }
        return memberEntity;
    }
}